package MapReduce.Demo9_Grouping_Grade;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;
import utils.JobSubmit;

import java.io.IOException;


/**
 * @Author lixinlei
 * @Date 2023/3/21 10:33
 */
public class GradeApp {

    public static class GMapper extends Mapper<Text,Text,Text, IntWritable>{

        IntWritable outValue = new IntWritable();

        @Override
        protected void map(Text key, Text value, Mapper<Text, Text, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            outValue.set(Integer.parseInt(value.toString()));

            context.write(key,outValue);
        }
    }

    public static class GReducer extends Reducer<Text,IntWritable,Text,IntWritable>{

        IntWritable outValue = new IntWritable();

        @Override
        protected void reduce(Text key, Iterable<IntWritable> values, Reducer<Text, IntWritable, Text, IntWritable>.Context context) throws IOException, InterruptedException {

            int gradeSum = 0;
            for (IntWritable value : values) {
                gradeSum += value.get();
            }

            outValue.set(gradeSum);

            context.write(key,outValue);
        }
    }

    public static void main(String[] args) {
        JobSubmit.submitBaseJob(
                GradeApp.class,
                args,
                KeyValueTextInputFormat.class,
                "group",
                GradeGroupingComparator.class
        );
    }

}
